package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "发射机监测项目数据记录表")
@Controller
public class WebHequipmentitemgroupController {

    @Autowired
    WebHequipmentitemgroupService webHequipmentitemgroupService;

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "创建'发射机监测项目数据记录表'表中一条信息", notes = "通过post方法请求，传入表中字段的对应信息，创建一条数据。并返回给View层")
    @RequestMapping(value = "/web_hequipmentitemgroups/new",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createWebHequipmentitemgroup(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "roomname", required = false) String roomname,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "owntype", required = false) String owntype,
            @RequestParam(value = "ownname", required = false) String ownname,
            @RequestParam(value = "alarmup", required = false) String alarmup,
            @RequestParam(value = "alarmdown", required = false) String alarmdown,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "ifalarm", required = false) String ifalarm,
            @RequestParam(value = "ifkey", required = false) String ifkey,
            @RequestParam(value = "showform", required = false) String showform,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        WebHequipmentitemgroup item = new WebHequipmentitemgroup();
        Date date = new Date();
        if( roomname != null ){
            item.setRoomname(roomname);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( owntype != null ){
            item.setOwntype(owntype);
        }
        if( ownname != null ){
            item.setOwnname(ownname);
        }
        if( alarmup != null ){
            item.setAlarmup(alarmup);
        }
        if( alarmdown != null ){
            item.setAlarmdown(alarmdown);
        }
        if( status != null ){
            item.setStatus(status);
        }
        if( ifalarm != null ){
            item.setIfalarm(ifalarm);
        }
        if( ifkey != null ){
            item.setIfkey(ifkey);
        }
        if( showform != null ){
            item.setShowform(showform);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
        WebHequipmentitemgroup iteams = webHequipmentitemgroupService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "删除'发射机监测项目数据记录表'表中的某条记录", notes = "根据url传入的数据id，删除整条记录。")
    @RequestMapping(value = "/web_hequipmentitemgroups/delete",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteWebHequipmentitemgroup(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "web_hequipmentitemgroup_id") Integer id
    ) {
        WebHequipmentitemgroup item = webHequipmentitemgroupService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
        }
        int delete = webHequipmentitemgroupService.delete(id);
        boolean result = delete!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "修改'发射机监测项目数据记录表'表中的某条记录", notes = "根据url传入的数据id，确定修改表中的某条记录，传入表中字段要修改的信息，不传代表不修改。并返回给View层")
    @RequestMapping(value = "/web_hequipmentitemgroups/edit",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editWebHequipmentitemgroup(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "roomname", required = false) String roomname,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "owntype", required = false) String owntype,
            @RequestParam(value = "ownname", required = false) String ownname,
            @RequestParam(value = "alarmup", required = false) String alarmup,
            @RequestParam(value = "alarmdown", required = false) String alarmdown,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "ifalarm", required = false) String ifalarm,
            @RequestParam(value = "ifkey", required = false) String ifkey,
            @RequestParam(value = "showform", required = false) String showform,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        WebHequipmentitemgroup item = webHequipmentitemgroupService.get(id);
        if (null == item) {
           return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
        }

        if( roomname != null ){
            item.setRoomname(roomname);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( owntype != null ){
            item.setOwntype(owntype);
        }
        if( ownname != null ){
            item.setOwnname(ownname);
        }
        if( alarmup != null ){
            item.setAlarmup(alarmup);
        }
        if( alarmdown != null ){
            item.setAlarmdown(alarmdown);
        }
        if( status != null ){
            item.setStatus(status);
        }
        if( ifalarm != null ){
            item.setIfalarm(ifalarm);
        }
        if( ifkey != null ){
            item.setIfkey(ifkey);
        }
        if( showform != null ){
            item.setShowform(showform);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
        int update =webHequipmentitemgroupService.update(item);
        boolean result = update!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "查询'发射机监测项目数据记录表'表中的某条记录", notes = "根据url传入的数据id，查询对应的一条数据。")
    @RequestMapping(value = "/getWebHequipmentitemgroupById",
                    method = {RequestMethod.GET},
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebHequipmentitemgroupById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {

            WebHequipmentitemgroup item = webHequipmentitemgroupService.get(id);
            if (null == item) {
                return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
            }

             return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);

    }

    @ApiOperation(value = "查询'发射机监测项目数据记录表'表中的多条记录或者新增某条记录", notes = "get传参：根据所需的字段进行匹配查询。数据数量取决于pageNo和pageSize；数据的先后顺序取决于sortItem，sortOrder； ")
    @RequestMapping(value = "/web_hequipmentitemgroups",
                    method = { RequestMethod.GET },
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebHequipmentitemgroups(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "roomname", required = false) String roomname,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "owntype", required = false) String owntype,
            @RequestParam(value = "ownname", required = false) String ownname,
            @RequestParam(value = "alarmup", required = false) String alarmup,
            @RequestParam(value = "alarmdown", required = false) String alarmdown,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "ifalarm", required = false) String ifalarm,
            @RequestParam(value = "ifkey", required = false) String ifkey,
            @RequestParam(value = "showform", required = false) String showform,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        WebHequipmentitemgroup item = new WebHequipmentitemgroup();
        if( roomname != null ){
            item.setRoomname(roomname);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( owntype != null ){
            item.setOwntype(owntype);
        }
        if( ownname != null ){
            item.setOwnname(ownname);
        }
        if( alarmup != null ){
            item.setAlarmup(alarmup);
        }
        if( alarmdown != null ){
            item.setAlarmdown(alarmdown);
        }
        if( status != null ){
            item.setStatus(status);
        }
        if( ifalarm != null ){
            item.setIfalarm(ifalarm);
        }
        if( ifkey != null ){
            item.setIfkey(ifkey);
        }
        if( showform != null ){
            item.setShowform(showform);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
       PageInfo<WebHequipmentitemgroup> list =  webHequipmentitemgroupService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
       return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);

    }
}
